<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="工号" name="jgh">
						<a-input v-model:value="searchFormState.jgh" placeholder="请输入工号"  style="width: 80px;"/>
					</a-form-item>
				</a-col>
                <a-col :span="4">
                    <a-form-item label="姓名" name="xm">
                        <a-input v-model:value="searchFormState.xm" placeholder="请输入姓名"  style="width: 100px;"/>
                    </a-form-item>
                </a-col>
				<a-col :span="6">
					<a-form-item label="部门" name="dwmc">
						<a-input v-model:value="searchFormState.dwmc" placeholder="请输入部门名称" />
					</a-form-item>
				</a-col>
                <a-col :span="6">
                    <a-form-item label="证件号码" name="sfzjh">
                        <a-input v-model:value="searchFormState.sfzjh" placeholder="请输入证件号码" />
                    </a-form-item>
                </a-col>
				<a-col :span="4">
					<a-form-item label="当前状态" name="dqzt">
						<a-input v-model:value="searchFormState.dqzt" placeholder="请输入当前状态" style="width: 80px;"/>
					</a-form-item>
				</a-col>
                <a-col :span="6">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					<a-button style="margin: 0 8px" @click="syncupd">同步</a-button>
                </a-col>
            </a-row>
        </a-form>
		<div>
			<a-spin :spinning="impUploadLoading">

			</a-spin>
		</div>
		<a-alert v-if="impAlertStatus" type="info" :show-icon="false" banner closable @close="onImpClose" class="mt-3">
			<template #description>
				<p>同步总数：{{ impResultData.totalCount }} 条</p>
				<p>同步成功：{{ impResultData.successCount }} 条</p>
				<div v-if="impResultData.errorCount > 0">
					<p><span class="xn-color-red">失败条数：</span>{{ impResultData.errorCount }} 条</p>
					<a-table :dataSource="impResultErrorDataSource" :columns="impErrorColumns" size="small" />
				</div>
			</template>
		</a-alert>
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.jgh"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
<!--           <a-space>
                <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('jwJzgxxAdd')">
                    <template #icon><plus-outlined /></template>
                    新增
                </a-button>
                <xn-batch-button
                    v-if="hasPerm('jwJzgxxBatchDelete')"
                    buttonName="批量删除"
                       icon="DeleteOutlined"
                    :selectedRowKeys="selectedRowKeys"
                    @batchCallBack="deleteBatchJwJzgxx"
                />
            </a-space>-->
            </template>
<!--            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('jwJzgxxEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['jwJzgxxEdit', 'jwJzgxxDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteJwJzgxx(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('jwJzgxxDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>-->
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="jzgxx">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import jwJzgxxApi from '@/api/biz/jwJzgxxApi'
    let searchFormState = reactive({dqzt:'在职'})
    const searchFormRef = ref()
    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    const columns = [
		{
			title: '所属单位名称',
			dataIndex: 'dwmc'
		},
		{
			title: '工号',
			dataIndex: 'jgh'
		},
        {
            title: '姓名',
            dataIndex: 'xm'
        },
/*        {
            title: '曾用名',
            dataIndex: 'cym',
            ellipsis: true
        },
        {
            title: '性别',
            dataIndex: 'xb',
            ellipsis: true
        },
        {
            title: '性别代码',
            dataIndex: 'xbdm',
            ellipsis: true
        },
        {
            title: '出生日期',
            dataIndex: 'csrq',
            ellipsis: true
        },
        {
            title: '民族',
            dataIndex: 'mz',
            ellipsis: true
        },
        {
            title: '民族代码',
            dataIndex: 'mzdm',
            ellipsis: true
        },
        {
            title: '身份类型',
            dataIndex: 'sfzjlxm'
        },*/
        {
            title: '证件号码',
            dataIndex: 'sfzjh'
        },
/*        {
            title: '教工类型名称',
            dataIndex: 'jzglbmc',
            ellipsis: true
        },
        {
            title: '教工类别代码',
            dataIndex: 'jzglbdm',
            ellipsis: true
        },
        {
            title: '教师职称',
            dataIndex: 'jszc',
            ellipsis: true
        },
        {
            title: '教师职称代码',
            dataIndex: 'jszcdm',
            ellipsis: true
        },*/
        {
            title: '当前状态',
            dataIndex: 'dqzt'
        },
        {
            title: '状态代码',
            dataIndex: 'dqztdm'
        },

        {
            title: '单位代码',
            dataIndex: 'dwh'
        },
    ]
    // 操作栏通过权限判断是否显示
    /*if (hasPerm(['jwJzgxxEdit', 'jwJzgxxDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }*/
    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = JSON.parse(JSON.stringify(searchFormState));
		//if(searchFormParam.jgh=="" && searchFormParam.xm==""&&searchFormParam.sfzh=="")return;
        return jwJzgxxApi.jwJzgxxPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
	// 同步
	const impUploadLoading = ref(false)
	const impAlertStatus = ref(false)
	const impResultData = ref({})
	const impResultErrorDataSource = ref([])
	const syncupd = () => {
		impUploadLoading.value = true
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState));
		jwJzgxxApi.jwJzgxxSyncupd(searchFormParam).then((res) => {
			impAlertStatus.value = true
			impResultData.value = res
			impResultErrorDataSource.value = res.errorDetail
		})
			.finally(() => {
				impUploadLoading.value = false
			})
	}
	// 关闭同步提示
	const onImpClose = () => {
		impAlertStatus.value = false
	}
    // 删除
    const deleteJwJzgxx = (record) => {
        let params = [
            {
                jgh: record.jgh
            }
        ]
        jwJzgxxApi.jwJzgxxDelete(params).then(() => {
            table.value.refresh(true)
        })
    }
    // 批量删除
    const deleteBatchJwJzgxx = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                jgh: m
            }
        })
        jwJzgxxApi.jwJzgxxDelete(params).then(() => {
            table.value.clearRefreshSelected()
        })
    }
</script>
